#include "clib_thrd_pool.h"
#include "clib_cpu.h"

static clib_cpu_info_t thrd_cpu[CLIB_THRD_MAX] = {};

clib_thrd_pool_t*
clib_thrd_pool_create() 
{
    void *pool = malloc(sizof(clib_thrd_pool_t));
    if(pool) {
        memset(pool, 0, sizof(clib_thrd_pool_t));
    }
    return pool;
}

i32_t
clib_thrd_pool_init(clib_thrd_pool_t* pool, i32_t cnt) 
{
    clib_thrd_t *thrd;
    if(!pool || !cnt) {
        return -1;
    }

    for(i32_t i = 0; i < cnt; i++) {
        thrd = &pool->thrd[i];
        thrd->idx  = i;
        thrd->cpu  = thrd_cpu[idx].cpu;
        thrd->node = thrd_cpu[idx].node;
        clib_thrd_init(thrd);
    }

    return 0;
}

i32_t
clib_thrd_set_work_core(i32_t idx, i32_t cpu)
{
    clib_cpu_info_t* info;
    info = clib_get_cpu_info(cpu);
    if(!info) {
        return -1;
    }
    thrd_cpu[idx].cpu  = info->cpu;
    thrd_cpu[idx].node = info->node;

    return 0;
}

i32_t
clib_thrd_set_main_core(i32_t idx, i32_t cpu)
{
    return 0;
}


i32_t
clib_thrd_pool_join(clib_thrd_pool_t* pool) 
{
    if(!pool) {
        return -1;
    }

    for(i32_t i = 0; i < pool->thrd_cnt; i++) {
        clib_thrd_join(&pool->thrd[i]);
    }

    return 0;
}